package com.ruomeng.robot.util;

import java.util.Properties;

import javax.servlet.ServletContext;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.ServletContextAware;



/**
 * 
 * @author 陈泽权
 * 
 *         下午8:18:34
 */
public class SpringContextUtil implements ApplicationContextAware,
		ServletContextAware {

	private static ApplicationContext applicationContext;

	private static ServletContext servletContext;

	private static Properties properties;

	@Override
	public void setApplicationContext(
			final ApplicationContext applicationContext) throws BeansException {
		SpringContextUtil.applicationContext = applicationContext;
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public static <T> T getBean(final String name) {
		return (T) applicationContext.getBean(name);
	}

	public static <T> T getBean(final Class<T> clazz) {

		return (T) getBean(SysStringUtils.toLowerCase(clazz.getSimpleName(), 0,
				1));
	}

	@Override
	public void setServletContext(final ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public static ServletContext getServletContext() {
		return servletContext;
	}

	public static void setProperties(final Properties _properties) {
		properties = _properties;
	}

	public static Properties getProperties() {
		return properties;
	}

	public static <T> T getPropertiestValue(final String name) {
		return (T) properties.getProperty(name);
	}

}
